// pages/newxf/newxf.js

var formutils = require('../../utils/http.js');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    project: [],
    projectindex: 0,
    date:"2019-04-01",
    startDate:"2019-01-01",
    endDate:"2019-04-01",
    tuiguang:[],
    showModal: false,
    _tabIndex:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var dateY = new Date()
    var year = dateY.getFullYear()
    var month = dateY.getMonth()+1
    var day = dateY.getDate()
    this.setData({
      date: year + '-' + month + '-' + (day-1),
      endDate: year + '-' + month + '-' + day,
    })

    var that = this
    //获取项目
    wx.request({
      url: app.globalData.httpurl + 'v2/project',
      method: "GET",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {

      },
      success: function (res) {
        console.log(res.data)
        that.setData({
          project: res.data.data
        })
        that.loadData()
      }
    })

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  loadData:function(){
    var that = this
    that.setData({
      tuiguang: []
    })
    wx.request({
      url: app.globalData.httpurl + 'v2/xiaofei',
      method: "GET",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {
        project_id: that.data.project[that.data.projectindex].project_id,
        created_at: that.data.date + " 00:00:00"
      },
      success: function (res) {
        var datas = res.data.data; //res.data就是从后台接收到的值
        console.log(res.data)
        that.setData({
          tuiguang: datas.tuiguang
        })
      }
    })
  },
  bindproject(e) {
    this.setData({
      tuiguang:[],
      projectindex: e.detail.value
    })
    this.loadData()
  },
  bindDateChange(e) {
    this.setData({
      tuiguang:[],
      date: e.detail.value
    })
    this.loadData()
  },
  bindlistxf:function(e){
    var id = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.index
    var key = "tuiguang[" + index +"].xiaofei"
    var values = e.detail.value
    this.setData({
      [key]: e.detail.value
    })
  },
  bindlistdj:function(e){
    var id = e.currentTarget.dataset.id
    var index = e.currentTarget.dataset.index
    var key = "tuiguang[" + index + "].hit"
    this.setData({
      [key]: e.detail.value
    })
  },
  bindbtn:function(){
    console.log(this.data.qudao);
    var that = this
    wx.request({
      url: app.globalData.httpurl + 'v2/xiaofei',
      method: "POST",
      header: {
        "Authorization": "bearer " + app.globalData.access_token
      },
      data: {
        "tuiguang" : JSON.stringify(this.data.tuiguang)
      },
      success: function (res) {
        console.log(res.data)
        if(res.data.status == 1){
          that.setData({
            showModal: true
          })
        }else{
          wx.showToast({
            title: res.data.message,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  formadd: function (e) {
    let formid = e.detail.formId
    formutils.postformid(formid)
    wx.navigateTo({
      url: '/pages/menu/menus',
    })
  },
  /**
  * 弹出框蒙层截断touchmove事件
  */
  preventTouchMove: function () {
  },
  /**
    * 对话框继续添加点击事件
    */
  onCancel: function () {
    if (app.globalData.isHomeXiaoFei){
      wx.reLaunch({
        url: '/pages/home3/home3',
      })
    }else{
      wx.reLaunch({
        url: '/pages/home2/home2',
      })
    }
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    this.setData({
      showModal:false,
    })
    this.loadData()
  }
})